//
//  NetworkBaseSessionManager.m
//  SLDemo
//
//  Created by bocai on 2016/12/5.
//  Copyright © 2016年 Heracles. All rights reserved.
//

#import "NetworkBaseSessionManager.h"
#import <AFNetworking/AFNetworking.h>
#import "NetworkManager.h"

@interface NetworkBaseSessionManager ()

@property (strong, nonatomic) AFHTTPSessionManager *sessionManager;

@end

@implementation NetworkBaseSessionManager

+ (instancetype)sharedManager
{
    static id manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [self new];
    });
    return manager;
}

- (instancetype)init
{
    if (self = [super init]) {
        _sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:[NetworkManager sharedManager].baseURLString]];
//        _sessionManager.requestSerializer.timeoutInterval = [NetworkManager sharedManager].timeoutInterval;
//        _sessionManager.requestSerializer.cachePolicy = [NetworkManager sharedManager].cachePolicy;
//        _sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    }
    return self;
}

- (void)requestWithURLString:(NSString *)urlString
                  httpMethod:(NetworkHTTPMethod)httpMethod
                  parameters:(NSDictionary *)parameters
               callbackBlock:(NetworkBaseSessionCallbackBlock)callbackBlock
{
    id success, failure, progress;
    
    if (callbackBlock) {
        success = ^(NSURLSessionDataTask *task, id responseObject) {
            callbackBlock(responseObject, nil, nil);
        };
        failure = ^(NSURLSessionDataTask *task, NSError *error) {
            callbackBlock(nil, nil, error);
        };
        progress = ^(NSProgress *progress) {
            callbackBlock(nil, progress, nil);
        };
    }
    
    switch (httpMethod) {
        case NetworkHTTPMethodGET: {
            [_sessionManager GET:urlString parameters:parameters progress:progress success:success failure:failure];
            break;
        }
        case NetworkHTTPMethodPOST: {
            [_sessionManager POST:urlString parameters:parameters progress:progress success:success failure:failure];
            break;
        }
    }
}

- (void)uploadDatasWithURLString:(NSString *)urlString
                      parameters:(NSDictionary *)parameters
                           datas:(NSArray<NSData *> *)datas
                            name:(NSString *)name
                       fileNames:(NSArray<NSString *> *)fileNames
                        mimeType:(NSString *)mimeType
                   callbackBlock:(NetworkBaseSessionCallbackBlock)callbackBlock
{
    [_sessionManager.requestSerializer requestWithMethod:nil URLString:nil parameters:nil error:nil];
    
    [_sessionManager POST:urlString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [datas enumerateObjectsUsingBlock:^(NSData * _Nonnull data, NSUInteger idx, BOOL * _Nonnull stop) {
            [formData appendPartWithFileData:data name:name fileName:fileNames[idx] mimeType:mimeType];
        }];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        if (callbackBlock) {
            callbackBlock(nil, uploadProgress, nil);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (callbackBlock) {
            callbackBlock(responseObject, nil, nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (callbackBlock) {
            callbackBlock(nil, nil, error);
        }
    }];
}

@end
